using System;
using System.Data;
using System.Data.SqlClient;
using NCS.Library;

namespace NCS.Modules.CMS.Lib.Contents
{
	/// <summary>
	/// Summary description for AliaDB.
	/// </summary>
	public class AliaDB
	{
		public AliaDB()
		{
		}
		public static DataTable GetAll (int userid)  
		{
			DataTable retVal = null;
			SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
			SqlCommand dbCmd = new SqlCommand("CMS_Alias_GetAll", dbConn);
			dbCmd.Parameters.AddWithValue("@userid",userid);
			dbCmd.CommandType = CommandType.StoredProcedure;
			try
			{
				retVal = new DataTable() ;
				SqlDataAdapter da = new SqlDataAdapter(dbCmd);
				da.Fill(retVal);
			}
			finally
			{
				dbConn.Close();
			}
			return retVal; 
		}
		public static void Delete (int _alias_ID)
		{
			SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
			SqlCommand dbCmd = new SqlCommand("CMS_Alias_Delete", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;
			dbCmd.Parameters.AddWithValue("@Alias_ID",_alias_ID);
			try
			{
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
			}
			finally
			{
				dbConn.Close();
			}
		}
		public static int Insert (AliaInfo _aliaInfo)
		{
			SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
			SqlCommand dbCmd = new SqlCommand("CMS_Alias_Insert", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;
			dbCmd.Parameters.AddWithValue("@User_ID",_aliaInfo.User_ID);
			dbCmd.Parameters.AddWithValue("@Alias_Name",_aliaInfo.Alias_Name);
			dbCmd.Parameters.AddWithValue("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
			try 
			{
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
				return (int)dbCmd.Parameters["@RETURN_VALUE"].Value;
			}
			finally 
			{
				dbConn.Close();
			}
		}
 
		public static void Update (AliaInfo _aliaInfo)
		{
			SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
			SqlCommand dbCmd = new SqlCommand("CMS_Alias_Update", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;
			dbCmd.Parameters.AddWithValue("@Alias_ID",_aliaInfo.Alias_ID);
			dbCmd.Parameters.AddWithValue("@User_ID",_aliaInfo.User_ID);
			dbCmd.Parameters.AddWithValue("@Alias_Name",_aliaInfo.Alias_Name);
			try 
			{
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
			}
			finally
			{
				dbConn.Close();
			}
		}
 
		public static AliaInfo GetInfo ( int _alias_ID )
		{
			AliaInfo retVal = null;
			SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
			SqlCommand dbCmd = new SqlCommand("CMS_Alias_GetInfo", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;
			dbCmd.Parameters.AddWithValue("@Alias_ID", _alias_ID);
			try 
			{
				dbConn.Open();
				SqlDataReader dr = dbCmd.ExecuteReader();
				if (dr.Read()) 
				{
					retVal = new AliaInfo();
					retVal.Alias_ID=Convert.ToInt32(dr["Alias_ID"]);
					retVal.User_ID=Convert.ToInt32(dr["User_ID"]);
					retVal.Alias_Name=Convert.ToString(dr["Alias_Name"]);
				}
				if (dr != null)	dr.Close();
			}
			finally
			{
				dbConn.Close();
			}
			return retVal;
		}
	}
}
